import modelEnhance from '@/utils/modelEnhance';
import PageHelper from '@/utils/pageHelper';
import { noticeGlobal} from "@/utils/core";
import {instanceOf} from "prop-types";

/**
 * 当第一次加载完页面时为true
 * 可以用这个值阻止切换页面时
 * 多次初始化数据
 */
let LOADED = false;
export default modelEnhance({
  namespace: 'teachers',

  state: {
    pageData: PageHelper.create(),
    userList: [],
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        if (pathname === '/teachers' && !LOADED) {
          LOADED = true;
          dispatch({
            type: 'init'
          });
        }
      });
    }
  },

  effects: {
    // 进入页面加载
    *init({ payload }, { call, put, select }) {
      const { pageData } = yield select(state => state.teachers);
      yield put({
        type: 'getPageInfo',
        payload: {
          pageData: pageData.startPage(1, 10)
        }
      });
    },
    // 获取分页数据
    *getPageInfo({ payload }, { call, put }) {
      const { pageData } = payload;
      yield put.resolve({
        type:'@request',
        payload: {
          actionType: 'setTeacherList',
          url: '/api/teacher/allList',
          data: pageData.pageinfoData(),
        }
      })

    },
    // 保存 之后查询分页
    *save({ payload }, { call, put, select, take }) {
      const { values, success } = payload;
      const { pageData } = yield select(state => state.teachers);
      if (values.avatar && values.avatar[0].response) values.avatar = values.avatar[0].response.data.fileName; else values.avatar = null;

      // put是非阻塞的 put.resolve是阻塞型的
      yield put.resolve({
        type: '@request',
        payload: {
          notice: true,
          url: '/api/authManage/user/addNewUsers',
          data: {userList: [values], roleId: 2}
        }
      });

      yield put({
        type: 'getPageInfo',
        payload: { pageData }
      });
      success();
    },
    // 修改
    *update({ payload }, { call, put, select }) {
      const { values, success } = payload;
      const { pageData } = yield select(state => state.teachers);
      if (values.avata && values.avatar[0].response) values.avatar = values.avatar[0].response.data.fileName; else values.avatar = null;

      yield put.resolve({
        type: '@request',
        payload: {
          notice: true,
          url: '/api/authManage/user/update',
          data: values,
        }
      });

      yield put({
        type: 'getPageInfo',
        payload: { pageData }
      });
      success();
    },
    // 删除 之后查询分页
    *remove({ payload }, { call, put, select }) {
      const { records, success } = payload;
      const { pageData } = yield select(state => state.teachers);
      yield put({
        type: '@request',
        payload: {
          notice: true,
          method: 'DELETE',
          url: '/api/authManage/user/delete/userIds',
          data: {idList: records.map(item => item.id)}
        }
      });
      yield put({
        type: 'getPageInfo',
        payload: { pageData }
      });
      success();
    },
    *import({payload}, {call, put, select}){
      const {values, success, tempFile} = payload;
      const { pageData } = yield select(state => state.teachers);

      const data = new FormData();
      data.append('userTmpl', tempFile);

      yield put.resolve({
        type: '@request',
        payload: {
          notice:true,
          method: 'POST',
          headers: {'content-type': 'multipart/form-data'},
          url: '/api/authManage/user/importTeachers',
          data,
        }
      });

      yield put({
        type: 'getPageInfo',
        payload: { pageData }
      });

      success();
    },
    *updateIsActive({payload}, {call, put, select}){
      const {val, record } = payload;
      const { pageData } = yield select(state => state.teachers);

      yield put.resolve({
        type: '@request',
        payload:{
          notice: true,
          url: '/api/authManage/user/updateActiveStatus',
          data: {activeStatus: val, userIds: [record.id] }
        }
      });

      yield put({
        type: 'getPageInfo',
        payload: { pageData }
      });
    },
  },

  reducers: {
    setTeacherList_SUCCESS(state, {payload}){
      let { pageData } = state;
      return {
        ...state,
        pageData: pageData.setPage(payload),
      };
    }
  }
});
